package cc.catface.base.net.core.cookie;

import android.content.Context;

import androidx.annotation.NonNull;

import java.io.IOException;
import java.util.HashSet;

import cc.catface.commom.context.TApp;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/** cookie:请求头添加cookie */
public class AddCookiesInterceptor implements Interceptor {

    @NonNull
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request.Builder builder = chain.request().newBuilder();
        HashSet<String> preferences = (HashSet<String>) TApp.getInstance().getSharedPreferences("cookie-config", Context.MODE_PRIVATE).getStringSet("cookie", null);
        if (null != preferences) {
            for (String cookie : preferences) {
                builder.addHeader("Cookie", cookie);
            }
        }
        return chain.proceed(builder.build());
    }
}